package com.open.harmony.business.service.serviceImpl;


import com.open.harmony.business.entity.User;
import com.open.harmony.business.mapper.UserMapper;
import com.open.harmony.business.service.UserService;
import com.open.harmony.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    @Autowired
    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public Result login(String userAccount, String userPassword) {
        if(Objects.equals(userAccount, "") || Objects.equals(userPassword, "")
        ||Objects.isNull(userAccount)||Objects.isNull(userPassword)){
            return Result.error("账号或密码不能为空");
        }
        User user = userMapper.login(userAccount, userPassword);
        if(user == null){
            return Result.error("账号或密码错误");
        }
        return Result.success(user);
    }

    @Override
    public Result register(User user) {
        Integer accountCnt = userMapper.findUserAccount(user.getUserAccount());
        if(accountCnt != 0){
            return Result.error("账号已存在");
        }
        Integer nameCnt = userMapper.findUserName(user.getUserName());
        if(nameCnt != 0){
            return Result.error("用户名已存在");
        }
        Integer result = userMapper.register(user);
        if(result == 0){
            return Result.error("注册失败");
        }
        return Result.success("注册成功");
    }

    @Override
    public Result updateImg(Integer userId, String userImg) {
        Integer result = userMapper.updateImg(userId, userImg);
        if(result == 0){
            return Result.error("更新图片失败");
        }
        return Result.success("更新图片成功");
    }
}
